package cn.hu.system.service.impl;

import cn.hu.system.domain.Role;
import cn.hu.system.mapper.MenuMapper;
import cn.hu.system.mapper.PermissionMapper;
import cn.hu.system.mapper.RoleMapper;
import cn.hu.system.service.IRoleService;
import cn.hu.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Hu
 * @since 2022-12-26
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    @Transactional
    public void add(Role role) {
        //删除此role对应的所有menu编辑的时候必要
//        menuMapper.deleteByRoleId();
        roleMapper.add(role);
        //保存t_role_menu
        roleMapper.addRoleMenu(role.getId(),role.getMenus());
        //删除此role对应的所有permission 编辑的时候必要
//        permissionMapper.deleteByRoleId();

        //保存t_role_permission
        roleMapper.addRolePermission(role.getId(),role.getPermissions());

    }
}
